const app = getApp();
const util = require('../../../utils/util');
Page({

  data: {
    typeList: [],
    goodsImages: [],
    goodsCoverImage: [],
    goodsname: '',
    goodsprice: 0,
    price: 0,
    goodsnumber: 1,
    typeId: 1,
    goodsintro: '',
    tempImgList: [],
    // coverImages: ,
    coverImg: '',
    detailImgs: [],
    currentIndex: 0,
  },

  onLoad: function (options) {
  },
  onShow: function (options) {
    this.getCategory();
    // this.getNewPage();
  },
  // 获取商品分类信息
  getCategory() {
    wx.request({
      url: `${app.globalData.baseurl}category/`,
      method: 'GET',
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            typeList: res.data
          });
          // console.log(this.data.typeList);
        }
      },
      fail: (err) => {
        console.log('error!');
      }
    })
  },
  getType(event) {
    // console.log(event.currentTarget.dataset.index);
    let index = event.currentTarget.dataset.index;
    this.setData({
      currentIndex: event.currentTarget.dataset.index,
      typeId: this.data.typeList[index].sortid
    })
    // console.log(this.data.typeId);
  },
  // 添加商品到数据库
  addGood(event) {
    console.log(event);
  },
  // 选择商品的封面图
  chooseCoverImage() {
    let tempFilePaths = [];
    let list = this.data.goodsCoverImage;
    var that = this;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      success: res => {
        console.log(res);
        tempFilePaths = res.tempFiles[0].tempFilePath
        this.setData({
          goodsCoverImage: list.concat(tempFilePaths)
        })
        wx.uploadFile({
          url: 'http://localhost:5050/upload_image',
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          success(res) {
            const data = JSON.parse(res.data)
            console.log(data.data.url);
            that.setData({
              coverImg: data.data.url
            })
            console.log(that.data.coverImg);
          }
        })
      }
    })
  },
  // 选择详情图片
  chooseDetailImage() {
    let tempFilePaths = [];
    let list = this.data.goodsImages;
    var that = this;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      success: (res => {
        console.log(res);
        const temp = res.tempFiles;
        const tempList = [];
        // console.log(temp);
        for (var j = 0; j < temp.length; j++) {
          tempList.push(temp[j].tempFilePath)
        }
        this.setData({
          goodsImages: list.concat(tempList),
          tempImgList: list.concat(tempList)
        })
        console.log(this.data.tempImgList);
        // 加载到后端服务器静态资源文件夹
        for (let i in this.data.tempImgList) {
          console.log(this.data.tempImgList[`${i}`])
          wx.uploadFile({
            url: 'http://localhost:5050/upload_image',
            filePath: this.data.tempImgList[`${i}`],
            name: 'file',
            success(res) {
              const data = JSON.parse(res.data)
              console.log(data.data.url);
              that.setData({
                detailImgs: that.data.detailImgs.concat(data.data.url)
              })
              console.log(that.data.detailImgs);
            }
          })
        }

      })
    })

  },
  // 获取商品原价
  getGoodsOriPrice(event) {
    this.setData({
      price: event.detail.value
    })
  },
  // 上传详情图片到后端存储
  uploadImageDetail() {
    console.log(111)
    var that = this;
    for (let i in this.data.tempImgList) {
      console.log(222);
      wx.uploadFile({
        url: `${app.globalData.baseurl}goods/upload_img`,
        filePath: `this.data.tempImgList[${i}]`,
        success(res) {
          console.log(res);
          that.data.goodsCoverImages.push(res.fileId);
          that.setData({
            goodsCoverImages: that.data.goodsCoverImages
          })
        }
      })
    }
  },
  // 删除封面图片
  deleteCoverImage(event) {
    console.log(event.currentTarget.dataset.index);
    this.data.goodsCoverImages.splice(event.currentTarget.dataset.index, 1);
    this.setData({
      goodsCoverImages: this.data.goodsCoverImages
    })
  },
  // 删除详情图片
  deleteDetailImage(event) {
    console.log(event.currentTarget.dataset.index);
    this.data.goodsImages.splice(event.currentTarget.dataset.index, 1);
    this.setData({
      goodsImages: this.data.goodsImages
    })
  },
  // 获取商品名字
  getGoodsName(event) {
    console.log(event.detail.value);
    this.setData({
      goodsname: event.detail.value
    })
    console.log(this.data.goodsname);
  },
  // 获取商品价格
  getGoodsPrice(event) {
    this.setData({
      goodsprice: event.detail.value
    })
    console.log(this.data.goodsprice);

  },
  // 获取商品数量
  getGoodsNumber(event) {
    this.setData({
      goodsnumber: event.detail.value
    })
    console.log(this.data.goodsnumber);
  },
  // 获取商品描述
  getGoodsDescribe(event) {
    this.setData({
      goodsintro: event.detail.value
    })
    console.log(this.data.goodsintro);
  },
  // 发布商品
  publicGoods() {
    let that = this;
    // console.log("这是发布");
    if (app.globalData.userInfo == null) {
      wx.showToast({
        icon: 'error',
        title: '未登录',
      })
      return;
    }
    wx.request({
      url: `${app.globalData.baseurl}goods/public`,
      method: "POST",
      data: {
        openid: app.globalData.openid,
        goodsImages: this.data.detailImgs,
        // goodsCoverImages: this.data.goodsCoverImages,
        goodsCoverImage: this.data.coverImg,
        goodsname: this.data.goodsname,
        goodsprice: this.data.goodsprice,
        price: this.data.price,
        goodsnumber: this.data.goodsnumber,
        typeId: this.data.typeId,
        goodsintro: this.data.goodsintro,
        username: app.globalData.userInfo.nickName,
        time: util.formatTime(new Date),
      },
      success: res => {
        console.log(this.data.goodsname);
        console.log(res);
        console.log(res.data);
        wx.switchTab({
          url: '../../index/index',
        })
        that.setData({
          typeList: [],
          goodsImages: [],
          goodsCoverImage: [],
          goodsname: '',
          goodsprice: 0,
          price: 0,
          goodsnumber: 1,
          typeId: 1,
          goodsintro: '',
          tempImgList: [],
          // coverImages: ,
          coverImg: '',
          detailImgs: [],
          currentIndex: 0,
        })
      },
    })
  },
  // getNewPage() {

  // }
})